package com.sugon.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.sugon.dao.DevPlanRuleDao;
import com.sugon.entity.DevMaintenPlanEntity;
import com.sugon.entity.DevPlanRuleEntity;
import com.sugon.service.DevPlanRuleService;
import com.sugon.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 设备维护计划与规程关联配置表Service实现类
 *
 * @author sugon
 * @date 2020-05-19 09:42:50
 */
@Service("devPlanRuleService")
public class DevPlanRuleServiceImpl implements DevPlanRuleService {

    @Autowired
    private DevPlanRuleDao devPlanRuleDao;

    /**
     * queryObject
     *
     * @param id 主键
     * @return
     */
    @Override
    public DevPlanRuleEntity queryObject(String id) {
        return devPlanRuleDao.queryObject(id);
    }

    /**
     * queryList
     *
     * @param map 参数
     * @return
     */
    @Override
    public List<DevPlanRuleEntity> queryList(Map<String, Object> map) {
        return devPlanRuleDao.queryList(map);
    }

    /**
     * queryTotal
     *
     * @param map 参数
     * @return
     */
    @Override
    public int queryTotal(Map<String, Object> map) {
        return devPlanRuleDao.queryTotal(map);
    }

    /**
     * queryRelatedRuleList
     *
     * @param map
     * @return
     */
    @Override
    public List<DevMaintenPlanEntity> queryRelatedRuleList(Map<String, Object> map) {
        return devPlanRuleDao.queryRelatedRuleList(map);
    }

    /**
     * queryRelatedRuleTotal
     *
     * @param map
     * @return
     */
    @Override
    public int queryRelatedRuleTotal(Map<String, Object> map) {
        return devPlanRuleDao.queryRelatedRuleTotal(map);
    }

    /**
     * save
     *
     * @param devPlanRule 实体
     * @return
     */
    @Override
    public int save(DevPlanRuleEntity devPlanRule) {
        devPlanRule.setId(RandomUtil.randomString(32));
        devPlanRule.setCreatorId(ShiroUtils.getUserId());
        devPlanRule.setCreateDate(DateUtil.date());
        return devPlanRuleDao.save(devPlanRule);
    }

    /**
     * update
     *
     * @param devPlanRule 实体
     * @return
     */
    @Override
    public int update(DevPlanRuleEntity devPlanRule) {
        return devPlanRuleDao.update(devPlanRule);
    }

    /**
     * delete
     *
     * @param id
     * @return
     */
    @Override
    public int delete(String id) {
        return devPlanRuleDao.delete(id);
    }

    /**
     * deleteBatch
     *
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(String[] ids) {
        return devPlanRuleDao.deleteBatch(ids);
    }

    /**
     * updateDay
     *
     * @param rules
     * @return
     */
    @Override
    public int updateDay(List<Map<String, Object>> rules) {
        int total = 0;
        if (rules.size() > 0) {
//            for (Map<String, Object> rule : rules) {
//                String id = rule.get("id") + "";
//                String months = rule.get("monthNum") + "";
//                String days = rule.get("dayNum") + "";
//                String rateTypeS = rule.get("rateType") + "";
//                int month = StringUtils.isNotEmpty(months) ? Integer.parseInt(months) : 1;
//                int day = StringUtils.isNotEmpty(days) ? Integer.parseInt(days) : 1;
//                int rateType = StringUtils.isNotEmpty(rateTypeS) ? Integer.parseInt(rateTypeS) : 1;
//                DevPlanRuleEntity dpr = devPlanRuleDao.queryObject(id);
//                dpr.setMonNum(month);
//                dpr.setDayNum(day);
//                dpr.setRuleType(rateType);
//                devPlanRuleDao.update(dpr);
//                total++;
//            }
        }

        return total;
    }

    /**
     * 查看维护计划和维护规程是否已建立关联
     *
     * @param planId 维护计划ID
     * @param ruleId 维护规程ID
     * @return
     */
    @Override
    public boolean checkIsExists(String planId, String ruleId) {
        Map<String, Object> map = new HashMap<>(2);
        map.put("planId", planId);
        map.put("ruleId", ruleId);
        return devPlanRuleDao.queryTotal(map) > 0;
    }

    @Override
    public List<DevPlanRuleEntity> list(String planId, String ruleId) {
        Map<String, Object> map = new HashMap<>(2);
        map.put("planId", planId);
        map.put("ruleId", ruleId);
        return devPlanRuleDao.queryList(map);
    }
}
